﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace AntSimulationGui
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        [DllImport("..\\..\\..\\Debug\\AntSimulationDLL.dll", CallingConvention = CallingConvention.StdCall)]
        static public extern void SetParameters(int rows, int columns, int countOfAnts, int countOfFood, int countOfHives);

        [DllImport("..\\..\\..\\Debug\\AntSimulationDLL.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        [return: MarshalAs(UnmanagedType.LPWStr)]
        public static extern String SimulateStep();

        private DataContainer dc = new DataContainer();

        public MainWindow()
        {
            this.DataContext = dc;
            InitializeComponent();

        }

        private void btnSet_Click_1(object sender, RoutedEventArgs e)
        {
            SetParameters(dc.Rows, dc.Columns, dc.CountOfAnts, dc.CountOfFood, dc.CountOfHives);
            SetPlayGround();
            dc.ParameterButtonEnabled = false;
            dc.SimulateButtonEnabled = true;
        }

        private void SetPlayGround()
        {

            this.PlayGrid.RowDefinitions.Clear();
            this.PlayGrid.ColumnDefinitions.Clear();


            for (int rows = 0; rows < dc.Rows; rows++)
            {
                RowDefinition rd = new RowDefinition();
                this.PlayGrid.RowDefinitions.Add(rd);

            }
            for (int columns = 0; columns < dc.Columns; columns++)
            {
                ColumnDefinition cd = new ColumnDefinition();
                this.PlayGrid.ColumnDefinitions.Add(cd);
            }

            this.PlayGrid.InvalidateMeasure();
            this.PlayGrid.InvalidateArrange();
            this.PlayGrid.InvalidateVisual();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            dc.AntsCount = 0;
            dc.FoodCount = 0;
            PlayGrid.Children.Clear();
            string st = SimulateStep();
            if (dc.MessageBox)
                MessageBox.Show(st);
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(st);
            Canvas Hive = null;
            var height = MainGrid.RowDefinitions[0].ActualHeight;
            var width = MainGrid.ActualWidth;
            var xcount = PlayGrid.ColumnDefinitions.Count;
            var ycount = PlayGrid.RowDefinitions.Count;
            var FieldHeight = height / ycount;
            var FieldWidth = width / xcount;
            foreach (XmlNode Items in xdoc.ChildNodes)
            {
                foreach (XmlNode Item in Items.ChildNodes)
                {
                    if (Item.Name == "Ant")
                    {
                        var rect = new Canvas();
                        rect.Height = 5;
                        rect.Width = 5;
                        SolidColorBrush br = new SolidColorBrush(Color.FromRgb((byte)200, (byte)0, (byte)0));
                        rect.Background = br;
                        rect.SetValue(Grid.ColumnProperty, Int32.Parse(Item.Attributes[0].Value));
                        rect.SetValue(Grid.RowProperty, Int32.Parse(Item.Attributes[1].Value));
                        PlayGrid.Children.Add(rect);
                        dc.AntsCount++;
                    }

                    else if (Item.Name == "Hive")
                    {
                        Hive = new Canvas();
                        Hive.Height = 6;
                        Hive.Width = 6;
                        SolidColorBrush br = new SolidColorBrush(Color.FromRgb((byte)0, (byte)0, (byte)200));
                        Hive.Background = br;
                        Hive.SetValue(Grid.ColumnProperty, Int32.Parse(Item.Attributes[0].Value));
                        Hive.SetValue(Grid.RowProperty, Int32.Parse(Item.Attributes[1].Value));

                    }

                    else if (Item.Name == "Food")
                    {
                        var count = Int32.Parse(Item.Attributes[2].Value);
                        if (count < 10)
                            continue;
                        var rect = new Canvas();
                        rect.Height = 5;
                        rect.Width = 5;
                        SolidColorBrush br = new SolidColorBrush(Color.FromRgb((byte)0, (byte)200, (byte)0));
                        rect.Background = br;
                        rect.SetValue(Grid.ColumnProperty, Int32.Parse(Item.Attributes[0].Value));
                        rect.SetValue(Grid.RowProperty, Int32.Parse(Item.Attributes[1].Value));
                        PlayGrid.Children.Add(rect);
                        dc.FoodCount++;
                    }
                    else if (Item.Name == "Field")
                    {
                        var Pheromones = Int32.Parse(Item.Attributes[2].Value);
                        if (Pheromones < 1)
                            continue;
                        var rect = new Canvas();
                        rect.Height = FieldHeight;
                        rect.Width = FieldWidth;
                        SolidColorBrush br = new SolidColorBrush(Color.FromArgb((byte)Pheromones, (byte)255, (byte)255, (byte)0));

                        rect.Background = br;
                        rect.SetValue(Grid.ColumnProperty, Int32.Parse(Item.Attributes[0].Value));
                        rect.SetValue(Grid.RowProperty, Int32.Parse(Item.Attributes[1].Value));
                        PlayGrid.Children.Add(rect);


                    }
                }
            }
            PlayGrid.Children.Add(Hive); // Hive erst zum Schluss hinzufügen, damit Z-Pos mäßig über dem Food steht.
        }
    }
}
